package com.qdxht.dispenser.utils;

import android.app.Application;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import com.qdxht.dispenser.base.AppConfig;
import com.qdxht.dispenser.base.manager.ActivityManager;
import com.qdxht.dispenser.ui.activity.user.LoginActivity;
import com.qdxht.dispenser.ui.models.entity.UserInfoEntity;
import java.util.List;

/**
 * @author GJ
 * @Description:
 * @date 2022/6/11 16:40
 */
public class CommonUtil {

    public static void loginOut() {
        UserUtil.removeLoginEntity();
        UserUtil.removeUserInfo();
        Application application = ActivityManager.getInstance().getApplication();
        Intent intent = new Intent(application, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        application.startActivity(intent);
        // 销毁除了登录页之外的 Activity
        ActivityManager.getInstance().finishAllActivities(LoginActivity.class);
    }

    public static long getUserId() {
        UserInfoEntity userInfo = UserUtil.getUserInfo();
        if(userInfo != null){
            return userInfo.getUid();
        }
        return 0L;
    }

    /**
     * 获取视频文件截图
     * @param path 视频文件的路径
     * @return Bitmap 返回获取的Bitmap
     */
    public static Bitmap getVideoThumb(String path) {
        MediaMetadataRetriever media = new MediaMetadataRetriever();
        media.setDataSource(path);
        return media.getFrameAtTime();
    }


    public static void removeLoginConfEntity() {
        SpUtils.removeKey("loginConfEntity");
    }


    /**
     * 获取支付类型： 0 金币， 1人民币
     * @param itemTypeId
     * @return
     */
    public static int getOrderType(Integer itemTypeId){
        if (itemTypeId == null){
            return 0;
        } else if (itemTypeId == 13
                || itemTypeId == 15
        ){
            return 1;
        }
        return 0;
    }

    public static void saveSearchHistory(List<String> list) {
        SpUtils.putObject("searchHistory", list);
    }

    public static List<String> getSearchHistory() {
        return SpUtils.getObject("searchHistory", List.class);
    }

    public static void removeSearchHistory() {
        SpUtils.removeKey("searchHistory");
    }

    public static boolean copyStr(Context context, String copyStr) {
        try {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", copyStr);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            return true;
        } catch (Exception e) {
        }
        return false;
    }

    public static void removeAd() {
        SpUtils.removeKey("ad");
    }

    /**
     * 打开默认浏览器
     * @param context
     * @param url
     */
    public static void openBrowser(Context context, String url) {
        if(url.indexOf("http") == 0){
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(intent);
        }
    }


}
